package com.example.lj.uav.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.lj.uav.R;

import static com.example.lj.uav.MyApplication.mPref;

public class FlightRecordDialog extends Dialog implements View.OnClickListener {

    private Context mContext;
    private OnCloseListener listener;
    private int month = 1;

    public FlightRecordDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    public FlightRecordDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    public FlightRecordDialog(Context context, int themeResId, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.listener = listener;
    }

    protected FlightRecordDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_flightrecord);
        setCanceledOnTouchOutside(true);
        initView();
    }

    private void initView() {
        final RadioButton rb_1 = findViewById(R.id.dlalog_flightrecordtv_rb_1);
        final RadioButton rb_2 = findViewById(R.id.dlalog_flightrecordtv_rb_2);
        final RadioButton rb_3 = findViewById(R.id.dlalog_flightrecordtv_rb_3);
        month = mPref.getInt("flight_recorders_month", 1);
        if (month == 1)
            rb_1.setChecked(true);
        else if (month == 6)
            rb_2.setChecked(true);
        else if (month == 120)
            rb_3.setChecked(true);
        ((RadioGroup) findViewById(R.id.dlalog_flightrecordtv_rg_group)).setOnCheckedChangeListener(
                new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        rb_1.setOnClickListener(FlightRecordDialog.this);
                        rb_2.setOnClickListener(FlightRecordDialog.this);
                        rb_3.setOnClickListener(FlightRecordDialog.this);

                    }
                });
        findViewById(R.id.dlalog_flightrecordtv_synchronization).setOnClickListener(FlightRecordDialog.this);
        findViewById(R.id.dlalog_flightrecordtv_clear).setOnClickListener(FlightRecordDialog.this);
        findViewById(R.id.dlalog_flightrecordtv_cancel).setOnClickListener(FlightRecordDialog.this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dlalog_flightrecordtv_rb_1:
                month = 1;
                break;
            case R.id.dlalog_flightrecordtv_rb_2:
                month = 6;
                break;
            case R.id.dlalog_flightrecordtv_rb_3:
                month = 120;
                break;
            case R.id.dlalog_flightrecordtv_synchronization:
                if (listener != null) {
                    listener.onClick(this, 0);
                    mPref.edit().putInt("flight_recorders_month", month).apply();
                }
                break;
            case R.id.dlalog_flightrecordtv_clear:
                if (listener != null) {
                    listener.onClick(this, 1);
                }
                break;
            case R.id.dlalog_flightrecordtv_cancel:
                if (listener != null) {
                    listener.onClick(this, 2);
                }
                break;
        }
    }

    public interface OnCloseListener {
        void onClick(Dialog dialog, int id);
    }
}
